package com.xy.bizportdemo.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.StrictMode;

/**
 * Created by HuangYueHui on 2017/7/19.
 */

public class StrictModeUtil {

    public static void init(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(BizportContentUtil.SETTING_FILE_NAME,
                Context.CONTEXT_IGNORE_SECURITY);
        boolean isStrictMode = sharedPreferences.getBoolean(BizportContentUtil.STRICT_MODE, false);
        if (isStrictMode) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectCustomSlowCalls() //API等级11，使用StrictMode.noteSlowCode
                    .detectDiskReads()
                    .detectDiskWrites()
                    .detectNetwork()   // or .detectAll() for all detectable problems
                    .penaltyDialog() //弹出违规提示对话框
                    .penaltyLog() //在Logcat 中打印违规异常信息
                    .penaltyFlashScreen() //API等级11
                    .build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectLeakedSqlLiteObjects()
                    .detectLeakedClosableObjects() //API等级11
                    .penaltyLog()
                    .penaltyDeath()
                    .build());
        }
    }

    public static void open(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(BizportContentUtil.SETTING_FILE_NAME,
                Context.CONTEXT_IGNORE_SECURITY);
        boolean isStrictMode = sharedPreferences.getBoolean(BizportContentUtil.STRICT_MODE, false);
        if (isStrictMode) {
            //StrictMode UI线程中是否有网络请求操作
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectNetwork()//UI线程中是否有网络请求操作
                    .detectDiskReads()//磁盘读检查
                    .detectDiskWrites()//磁盘写检查
                    .penaltyLog()
                    .build());


            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                            .detectActivityLeaks() //用户检查 Activity 的内存泄露情况
                            .detectLeakedClosableObjects() //detectLeakedClosableObjects()
                            .detectLeakedSqlLiteObjects() //泄露的Sqlite对象
                            .penaltyLog()
                            .build()
            );
        }
    }
}
